package com.example.demo.handler;

import com.example.demo.service.UserServiceImpl;
import com.lagou.service.UserService;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Service;


@Service
public class UserServerHandler extends ChannelInboundHandlerAdapter implements ApplicationContextAware {

    private static ApplicationContext applicationContext = null;

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

        String susses = applicationContext.getBean(UserServiceImpl.class).success(msg);
        ctx.writeAndFlush(susses);


    }

    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if (UserServerHandler.applicationContext == null) {
            UserServerHandler.applicationContext = applicationContext;
        }
    }

    /**
     * 通过name获取 Bean.
     */
    public static Object getBean(String name) {
        return getApplicationContext().getBean(name);
    }
}
